@{
    ViewData["Title"] = "编辑维修单";
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>@ViewData["Title"]</title>
    <link href="~/layui/css/layui.css" rel="stylesheet" />
    <style>
        .form-container {
            padding: 20px;
            max-width: 800px;
            margin: 0 auto;
        }
        .required {
            color: red;
        }
        .status-section {
            margin-top: 20px;
            border-top: 1px solid #e6e6e6;
            padding-top: 20px;
        }
    </style>
</head>
<body>
    @Html.AntiForgeryToken()
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-header">
                <span>编辑维修单</span>
            </div>
            <div class="layui-card-body">
                <div class="form-container">
                    <form class="layui-form" lay-filter="repairEditForm">
                        <input type="hidden" name="id" id="repairId">
                        
                        <div class="layui-row">
                            <div class="layui-col-md6">
                                <div class="layui-form-item">
                                    <label class="layui-form-label"><span class="required">*</span>维修单编号</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="repairCode" readonly class="layui-input">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md6">
                                <div class="layui-form-item">
                                    <label class="layui-form-label"><span class="required">*</span>维修单名称</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="repairName" lay-verify="required|repairName" placeholder="请输入维修单名称" class="layui-input">
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="layui-row">
                            <div class="layui-col-md6">
                                <div class="layui-form-item">
                                    <label class="layui-form-label"><span class="required">*</span>设备编号</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="deviceCode" lay-verify="required" placeholder="请输入设备编号" class="layui-input">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md6">
                                <div class="layui-form-item">
                                    <label class="layui-form-label"><span class="required">*</span>设备名称</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="deviceName" lay-verify="required" placeholder="请输入设备名称" class="layui-input">
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="layui-row">
                            <div class="layui-col-md6">
                                <div class="layui-form-item">
                                    <label class="layui-form-label"><span class="required">*</span>报修日期</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="warrantyDate" lay-verify="required" class="layui-input" id="warrantyDatePicker">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md6">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">维修日期</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="repairDate" class="layui-input" id="repairDatePicker">
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="layui-row">
                            <div class="layui-col-md6">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">验收日期</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="acceptanceDate" class="layui-input" id="acceptanceDatePicker">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md6">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">维修人员</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="repairPersonnel" placeholder="请输入维修人员" class="layui-input">
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="layui-row">
                            <div class="layui-col-md6">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">验收人</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="acceptancePerson" placeholder="请输入验收人" class="layui-input">
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="layui-form-item">
                            <label class="layui-form-label">维修结果</label>
                            <div class="layui-input-block">
                                <textarea name="repairResults" placeholder="请输入维修结果" class="layui-textarea" maxlength="500"></textarea>
                            </div>
                        </div>

                        <!-- 状态管理部分 -->
                        <div class="status-section">
                            <div class="layui-form-item">
                                <label class="layui-form-label">单据状态</label>
                                <div class="layui-input-block">
                                    <select name="documentStatus" lay-filter="statusChange">
                                        <option value="0">待维修</option>
                                        <option value="1">维修中</option>
                                        <option value="2">待验收</option>
                                        <option value="3">已完成</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        
                        <div class="layui-form-item" style="text-align: center; margin-top: 30px;">
                            <button type="button" class="layui-btn" lay-submit lay-filter="submitForm">保存</button>
                            <button type="button" class="layui-btn layui-btn-primary" id="cancelBtn">取消</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script src="~/layui/layui.js"></script>
    <script>
        layui.use(['form', 'layer', 'laydate'], function () {
            var form = layui.form;
            var layer = layui.layer;
            var laydate = layui.laydate;
            var $ = layui.jquery;
            
            var repairData = null;
            var repairId = null;

            // 获取URL参数
            function getUrlParam(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]);
                return null;
            }

            // 初始化页面
            function initPage() {
                var id = getUrlParam('id');
                if (id) {
                    repairId = id;
                    loadRepairData(id);
                } else {
                    layer.msg('缺少维修单ID参数');
                    return;
                }
            }

            // 初始化日期选择器
            laydate.render({
                elem: '#warrantyDatePicker',
                type: 'date'
            });

            laydate.render({
                elem: '#repairDatePicker',
                type: 'date'
            });

            laydate.render({
                elem: '#acceptanceDatePicker',
                type: 'date'
            });

            // 加载维修单数据
            function loadRepairData(id) {
                var loadIndex = layer.load();
                $.ajax({
                    url: '/Repair/GetRepair',
                    type: 'GET',
                    data: { id: id },
                    success: function (res) {
                        layer.close(loadIndex);
                        if (res.success) {
                            repairData = res.data;
                            bindFormData(repairData);
                        } else {
                            layer.msg('获取维修单数据失败: ' + res.message);
                        }
                    },
                    error: function (xhr) {
                        layer.close(loadIndex);
                        console.error('获取维修单数据错误:', xhr);
                        layer.msg('服务器错误: ' + (xhr.responseJSON && xhr.responseJSON.message ? xhr.responseJSON.message : xhr.statusText));
                    }
                });
            }

            // 绑定表单数据
            function bindFormData(data) {
                // 处理日期格式
                if (data.warrantyDate) {
                    data.warrantyDate = new Date(data.warrantyDate).toLocaleDateString();
                }
                if (data.repairDate) {
                    data.repairDate = new Date(data.repairDate).toLocaleDateString();
                }
                if (data.acceptanceDate) {
                    data.acceptanceDate = new Date(data.acceptanceDate).toLocaleDateString();
                }
                
                // 先设置表单值
                form.val('repairEditForm', data);
                
                // 重新渲染表单组件
                setTimeout(function() {
                    form.render();
                }, 100);
            }

            // 自定义验证规则
            form.verify({
                repairName: function(value) {
                    if (!value) {
                        return '维修单名称不能为空';
                    }
                    if (value.length > 100) {
                        return '维修单名称长度不能超过100个字符';
                    }
                }
            });

            // 状态变更监听
            form.on('select(statusChange)', function(data) {
                var status = parseInt(data.value);
                var currentDate = new Date().toLocaleDateString();
                
                // 根据状态自动填充相关日期
                if (status === 1) { // 维修中
                    if (!$('input[name="repairDate"]').val()) {
                        $('input[name="repairDate"]').val(currentDate);
                    }
                } else if (status === 3) { // 已完成
                    if (!$('input[name="repairDate"]').val()) {
                        $('input[name="repairDate"]').val(currentDate);
                    }
                    if (!$('input[name="acceptanceDate"]').val()) {
                        $('input[name="acceptanceDate"]').val(currentDate);
                    }
                }
            });

            // 表单提交
            form.on('submit(submitForm)', function (formData) {
                var loadIndex = layer.load();
                var postData = formData.field;
                postData.id = repairId;

                console.log("提交数据:", postData);

                $.ajax({
                    url: '/Repair/UpdateRepair',
                    type: 'POST',
                    data: {
                        ...postData,
                        __RequestVerificationToken: $('input[name="__RequestVerificationToken"]').val()
                    },
                    success: function (res) {
                        layer.close(loadIndex);
                        if (res.success) {
                            layer.msg('更新成功', {
                                icon: 1,
                                time: 2000
                            }, function() {
                                // 关闭窗口
                                if (window.parent && window.parent.layer) {
                                    window.parent.layer.closeAll();
                                } else {
                                    window.location.href = '/Repair/Index';
                                }
                            });
                        } else {
                            layer.msg('更新失败: ' + res.message);
                        }
                    },
                    error: function (xhr) {
                        console.error('提交请求错误:', xhr);
                        layer.close(loadIndex);
                        layer.msg('服务器错误: ' + (xhr.responseJSON && xhr.responseJSON.message ? xhr.responseJSON.message : xhr.statusText));
                    }
                });
                return false;
            });

            // 取消按钮
            $('#cancelBtn').on('click', function () {
                if (window.parent && window.parent.layer) {
                    window.parent.layer.closeAll();
                } else {
                    window.location.href = '/Repair/Index';
                }
            });

            // 初始化页面
            initPage();
        });
    </script>
</body>
</html> 